package com.only.common.exception;

import cn.hutool.core.util.StrUtil;

/**
 * @author only
 * @date 2023/10/17
 * @description
 */
public class BizExceptionFactory {
    private static final String ResourceNotFoundCode = "404";
    private static final String BadRequestCode = "400";
    private static final String SystemErrorCode = "500";
    private static final String PermissionDenied = "403";
    private static final String ResourceConflictCode = "409";

    public static BizException badRequestException(String bizErrorMsg, String causeMsg) {
        return new BizException(BadRequestCode, bizErrorMsg, new Throwable(causeMsg));
    }

    public static BizException badRequestException(String bizErrorMsg) {
        return new BizException(BadRequestCode, bizErrorMsg, new Throwable(bizErrorMsg));
    }

    public static BizException resourceNotFoundException(String bizErrorMsg, String causeMsg) {
        return new BizException(ResourceNotFoundCode, bizErrorMsg, new Throwable(causeMsg));
    }

    public static BizException resourceNotFoundException(String bizErrorMsg) {
        return new BizException(ResourceNotFoundCode, bizErrorMsg, new Throwable(bizErrorMsg));
    }

    public static BizException invalidEnumValue(String code, String className) {
        return badRequestException("无效的枚举值", StrUtil.format("无效的枚举值: {}, enum: {}", code, className));
    }

    public static BizException permissionDenied() {
        return new BizException(PermissionDenied, "无权限访问");
    }

    public static BizException resourceConflictException(String bizErrorMsg, String causeMsg) {
        return new BizException(ResourceConflictCode, bizErrorMsg, new Throwable(causeMsg));
    }

    public static BizException resourceConflictException(String bizErrorMsg) {
        return new BizException(ResourceConflictCode, bizErrorMsg, new Throwable(bizErrorMsg));
    }

    /**
     * 系统异常
     * @param causeMsg
     * @return
     */
    public static BizException systemExceptionWithDefaultMsg(String causeMsg) {
        return new BizException(SystemErrorCode, "系统未知异常，请稍后再试！", new Throwable(causeMsg));
    }

    public static BizException systemException(String bizErrorMsg) {
        return new BizException(SystemErrorCode, bizErrorMsg, new Throwable(bizErrorMsg));
    }

    public static BizException systemException(String bizErrorMsg, String causeMsg) {
        return new BizException(SystemErrorCode, bizErrorMsg, new Throwable(causeMsg));
    }
}
